﻿//---------------------------------------------------------------------------
// <summary>
// Represents the event log that InfoPath uses in its postback bodies.
// </summary>
//---------------------------------------------------------------------------

namespace InfoPathExtractionRules
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;

    /// <summary>
    /// Represents the event log that InfoPath uses in its postback bodies.
    /// An event log has two main parts: EventLogInfo and Events.
    /// EventLogInfo contains metadata about the form that is currently being edited.
    /// Events contain a record of the actions that occured in the browser.
    /// The event log is played back on the server side to recreate the state of the form.
    /// </summary>
    public class EventLog
    {
        /// <summary>
        /// The space character, used as a separator character in the postback request string body.
        /// </summary>
        internal const char EventLogSeparator = ' ';

        /// <summary>
        /// The semi-colon character, used as a separator character in the postback request string body.
        /// </summary>
        internal const char EventLogItemSeparator = ';';

        /// <summary>
        /// Array of the event log parts
        /// </summary>
        private string[] eventLogParts;

        /// <summary>
        /// Holds the event log information.
        /// </summary>
        private EventLogInformation eventLogInformation;

        /// <summary>
        /// Initializes a new instance of the EventLog class.
        /// Represents the entire IPFS event log.
        /// </summary>
        /// <param name="eventLog">String of the event log.</param>
        public EventLog(string eventLog)
        {
            if (!eventLog.EndsWith(" ", StringComparison.Ordinal))
            {
                Trace.TraceWarning("Event log does not end with a space character.");
            }

            if (eventLog.EndsWith("  ", StringComparison.Ordinal))
            {
                Trace.TraceWarning("Event log ends with more than one space character.");
            }

            this.eventLogParts = eventLog.Split(EventLogSeparator);
            if (this.eventLogParts == null || this.eventLogParts.Length <= 0)
            {
                throw new ArgumentException("eventLogParts null or empty");
            }

            this.eventLogInformation = new EventLogInformation(this.eventLogParts[1]);
        }

        /// <summary>
        /// Gets or sets the eventLogInfo object of this event log.
        /// </summary>
        /// <value>
        /// The eventLogInfo object of this event log.
        /// </value>
        public EventLogInformation EventLogInfo
        {
            get
            {
                if (this.eventLogInformation == null)
                {
                    this.eventLogInformation = new EventLogInformation(this.eventLogParts[1]);
                }

                return this.eventLogInformation;
            }

            set
            {
                this.eventLogInformation = value;
            }
        }

        /// <summary>
        /// Builds an event log string.
        /// </summary>
        /// <returns>String with both the event log info and the events.</returns>
        public override string ToString()
        {
            this.eventLogParts[1] = this.eventLogInformation.ToString();
            return String.Join(EventLogSeparator.ToString(), this.eventLogParts);
        }
    }
}
